package com.ruoyi.web.controller.zhuangxiucontroller.shopController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.BrandInfo;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductInfo;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductLabel;
import com.ruoyi.system.domain.zhuangxiumain.vo.ProductInfoVo;
import com.ruoyi.system.service.zhaungxiuservice.shopService.ProductInfoService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.ProductLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(value = "商品信息接口", tags = {"商品信息接口"})
@RestController
@RequestMapping("/shopping/goods")
public class ProductInfoController extends BaseController {

    @Autowired(required = false)
    private ProductInfoService productInfoService;

    @Autowired(required = false)
    private ProductLabelService productLabelService;

    /**
     * 查询商品信息列表
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "查询商品信息列表")
    @PreAuthorize("@ss.hasPermi('shopping:goods:listpi')")
    @GetMapping("/listpi")
    public TableDataInfo list(ProductInfo productInfo)
    {

        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        return getDataTable(list);
    }



    /**
     * 导出商品信息列表
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "商品信息")
    @PreAuthorize("@ss.hasPermi('shopping:goods:exportpi')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/exportpi")
    public AjaxResult export(ProductInfo productInfo)
    {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取商品信息详细信息
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "获取商品信息详细信息")
    @PreAuthorize("@ss.hasPermi('shopping:goods:querypi')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Integer productId)
    {
        return AjaxResult.success(productInfoService.selectProductInfoById(productId));
    }

    @PreAuthorize("@ss.hasPermi('shopping:goods:getInfolab')")
    @GetMapping(value = "/getInfolab/{labelId}")
    public AjaxResult getInfolab(@PathVariable("labelId") Integer labelId)
    {
        return AjaxResult.success(productInfoService.selectProductLabelById(labelId));
    }

    /**
     * 新增商品信息
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "新增商品信息")
    @PreAuthorize("@ss.hasPermi('shopping:goods:addpi')")
    @Log(title = "新增商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductInfo productInfo)
    {
        Integer numBrand = productInfo.getNumBrand();
        productInfo.setBrandId(numBrand);
        Integer numCategory = productInfo.getNumCategory();
        productInfo.setCategoryId(numCategory);
        Integer numSupper = productInfo.getNumSupper();
        productInfo.setSupplierId(numSupper);
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        System.err.println("**********88888888888888"+num);
        productInfo.setProductCore(num);
        productInfo.setIndate(new Date());
        productInfo.setModifiedTime(new Date());
        productInfoService.insertProductInfo(productInfo);
        return AjaxResult.success("操作成功");
    }

    /**
     * 修改商品信息
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "修改商品信息")
    @PreAuthorize("@ss.hasPermi('shopping:goods:editpi')")
    @Log(title = "修改商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除商品信息
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "删除商品信息")
    @PreAuthorize("@ss.hasPermi('shopping:goods:removepi')")
    @Log(title = "删除商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Integer[] productIds)
    {
        return toAjax(productInfoService.deleteProductInfoByIds(productIds));
    }

    @PreAuthorize("@ss.hasPermi('shopping:goods:removelab')")
    @Log(title = "删除商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/removelab/{labelId}")
    public AjaxResult removelab(@PathVariable Integer[] labelId)
    {
        return toAjax(productInfoService.deleteProductLabelByIds(labelId));
    }


    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "修改上下架状态")
    @PreAuthorize("@ss.hasPermi('shopping:goods:publishStatus')")
    @Log(title = "修改上下架状态", businessType = BusinessType.UPDATE)
    @PutMapping("/publishStatus")
    public AjaxResult publishStatusUpdate(@RequestBody ProductInfo productInfo){
        return toAjax(productInfoService.publishStatusUpdate(productInfo));
    }

    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "修改审核状态")
    @PreAuthorize("@ss.hasPermi('shopping:goods:auditStatus')")
    @Log(title = "修改审核状态", businessType = BusinessType.UPDATE)
    @PutMapping("/auditStatus")
    public AjaxResult auditStatusUpdate(@RequestBody ProductInfo productInfo){
        return toAjax(productInfoService.auditStatusUpdate(productInfo));
    }


    @PreAuthorize("@ss.hasPermi('shopping:goods:listlabelfindall')")
    @GetMapping("/listlabelfindall/{productPicId}")
    public List<ProductLabel> listlabelfindall(@PathVariable("productPicId") Integer productPicId){
        return productInfoService.selectProductLabelList(productPicId);
    }

    @PreAuthorize("@ss.hasPermi('shopping:goods:listlabelfindproduct')")
    @GetMapping(value = "/listlabelfindproduct/{productId}")
    public List<ProductLabel> listlabelfindproduct(@PathVariable("productId") Integer productId){
        return productInfoService.listlabelproduct(productId);
    }



    /**
     * 获取商品标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:goods:gglabelquery')")
    @GetMapping(value = "/label/{labelId}")
    public AjaxResult labelgetInfo(@PathVariable("labelId") Integer labelId)
    {
        return AjaxResult.success(productInfoService.selectProductLabelById(labelId));
    }








}
